package thead;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * 使用线程池
 */
public class Demo21 {
    public static void main(String[] args) {
        //ExecutorService pool = Executors.newScheduledThreadPool();
        /*
            这个是类似定时器,延迟或者定时多少秒后执行
         */

        //ExecutorService pool = Executors.newSingleThreadExecutor();
        /*
            只有一个线程的线程池,用的不多
         */
        //ExecutorService pool = Executors.newCachedThreadPool();
        /*
            Cached 缓存, 创建使用后不着急销毁,留备后续使用
            动态的,线程数量是动态的
         */
        ExecutorService pool = Executors.newFixedThreadPool(10);
        /*
            Fuxed 固定的,创建固定的线程,来实现任务
         */
        for(int i =0; i < 1000;i++) {
            int id = i;
           pool.submit(new Runnable() {
               @Override
               public void run() {
                   System.out.println("当前任务id: " + id + "  线程id: " + Thread.currentThread().getName());
               }
           });
        }
    }
}
